#include "NetPlugin.h"
#include "Log.h"
#include "Server/PluginsManager.h"
#include "Network/Client.h"
#include "Game/PlayerData.h"
#include "Subsystems/SubsystemPlayers.h"

#include "GameSingleton.h"
#include "Network/PackageManager.h"
#include "Network/Packages/MessagePackage.h"
#include "Network/PackageManager.h"

namespace PocketSurvival
{
    const std::string &NetPlugin::getPluginName()
    {
        static const std::string pluginName = "网络测试插件";
        return pluginName;
    }

    const std::string &NetPlugin::getIntroduce()
    {
        static const std::string introduce = "\n/net user (在线玩家) - 日志打印收到该玩家的数据包详情";
        return introduce;
    }

    uint32_t NetPlugin::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType NetPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void NetPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(cmdDataVec.size() == 2)
        {
            if(cmdDataVec[1] == "clear")
            {
                m_packageManager->ClearShowPackageClient();
                chatObj.sendMessage("net", fmt::format("清理掉数据包显示", cmdDataVec[2]));
                return;
            }
        }
        else if(cmdDataVec.size() >= 3)
        {
            if(cmdDataVec[1] == "user")
            {
                std::vector<PlayerData *> playerVec;
                GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>()->getPlayerDataByName(cmdDataVec[2], playerVec);
                if(playerVec.size() == 0)
                {
                    chatObj.sendMessage("net", fmt::format("玩家 {} 貌似不在线", cmdDataVec[2]));
                }
                else
                {
                    for(PlayerData *playerData : playerVec)
                    {
                        if(playerData->client != nullptr)
                        {
                            m_packageManager->AddShowPackageClient(playerData->client->addressHashCode);
                        }
                    }
                    chatObj.sendMessage("net", fmt::format("日志会打印名字叫 {} 的玩家的数据包详情", cmdDataVec[2]));
                }
                return;
            }
        }
        chatObj.sendMessage("net", "\n/net user (在线玩家) - 日志打印收到该玩家的数据包详情");
        return;
    }

    void NetPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/net");
    }

    void NetPlugin::load()
    {
        m_packageManager = GameSingleton::singletonPtr->getSingleton<PackageManager>();
    }

} // namespace PocketSurvival


using namespace PocketSurvival;

extern "C" void initialize()
{
    GameSingleton::singletonPtr->getSingleton<PluginsManager>()->registerPlugin<NetPlugin>();
    Log::info("成功加载 NetPlugin 插件");
}